import enUs from "./en-US.json";
import LocalizationContext from "./LocalizationContext";
import LocalizationMap from "./LocalizationMap";
import { FC, ReactElement, useState } from "react";

type SetLocalization = (i18n: LocalizationMap) => void;

interface LocalizationProviderProps {
  children: (setLocalization: SetLocalization) => ReactElement;
  localization?: LocalizationMap;
}

const LocalizationProvider: FC<LocalizationProviderProps> = ({
  children,
  localization,
}) => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const defaultI18n = (enUs as any) as LocalizationMap;

  const [i18nData, setI18nData] = useState(localization || defaultI18n);
  const setLocalization = (i18n: LocalizationMap) => setI18nData(i18n);

  return (
    <LocalizationContext.Provider value={i18nData}>
      {children(setLocalization)}
    </LocalizationContext.Provider>
  );
};

export default LocalizationProvider;
